<?php

namespace App\Kuafu\Mybank\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Shop\Model\Shop;
use App\Remote\Mybank\Remote;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;

class MybankOrderWithholdRefundRecordModel extends BaseModel
{
    use HasFactory, KfSoftDeletes, KfUid;

    protected $connection = 'mysql';
    protected $table      = "mybank_order_withhold_refund_record";

    protected $fillable = [
        'participantType',
        'participantId',
        'relateOrderNo',
        'relateTransNo',
        'refundOrderNo',
        'outOrderNo',
        'outRefundNo',
        'refundAmount',
        'freezeAmount',
        'availableAmount',
        'currency',
        'refundReason',
        'status',
        'memo',
        'submitDateTime',
        'refundFinishDate',
        'extInfo',
        'createdAt',
        'createdByUid',
        'errorCode',
        'errorDesc',
        'updatedAt',
        'isDeleted',
        'updatedByUid'
    ];

    /**
     * 获取总退款金额
     *
     * @return int
     */
    public function getTotalRefundAmountByRefundOrderNo(): int
    {
        return self::query()
                   ->where('relateOrderNo', $this->relateOrderNo)
                   ->where('status', MybankOrderWithholdRecordModel::WITHHOLD_STATUS_SUCCESS)
                   ->sum('refundAmount');
    }

    /**
     * @return HasOne
     */
    public function myBankOrderWithholdRecordRel(): HasOne
    {
        return $this->hasOne(MybankOrderWithholdRecordModel::class, "orderNo", "relateOrderNo");
    }

    public function getInfo(string $outOrderNo)
    {
        return self::query()->where('outOrderNo', $outOrderNo)->first();
    }

    public function updateById(int $id, array $data): int
    {
        return self::query()->where('id', $id)->update($data);
    }
}
